package com.ruoyi.web.controller.api;

import com.alibaba.fastjson.JSONObject;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.system.domain.LoginModal;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 */
@Api("巡检中心")
@Controller
@RequestMapping("/api")
public class LoginAPIController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(LoginAPIController.class);

    @Autowired
    ISysUserService userService;

    /**
     * 用户登录接口
     *
     * @param username
     * @param password
     * @return
     */
    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    @ResponseBody
    public AjaxResult ajaxLogin(String username, String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, false);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            SysUser sysUser = userService.selectUserByLoginName(username);
            List<SysRole> roles = sysUser.getRoles();
            LoginModal obj = new LoginModal();
            obj.setUserName(username);
            String hash = Md5Utils.hash(UUID.randomUUID().toString());
            obj.setToken(hash);
            CacheUtils.put(hash, hash);
            SysUser suser = userService.selectUserByLoginName(username);
            suser.setRemark(hash);
            userService.updateUserInfo(suser);
            List<String> collect = roles.stream().map(SysRole::getRoleKey).collect(Collectors.toList());
            obj.setRoleList(collect);
            System.out.println("token:" + hash);
            return AjaxResult.success("登录成功", obj);
        } catch (AuthenticationException e) {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage())) {
                msg = e.getMessage();
            }
            return AjaxResult.error(msg);
        }
    }


}
